package com.awesome.javademo.collection.sort;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.javademo.R;
import com.awesome.javademo.collection.sort.bean.Student;
import com.awesome.javademo.util.PinyinUtil;
import com.awesome.common.util.log.ShowLogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CompareActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compare);
        List<Student> studentList = new ArrayList<>();
        Student student1 = new Student("b");
        Student student2 = new Student("bz");
        Student student3 = new Student("a");
        Student student4 = new Student("ba");
        Student student5 = new Student("不行啊");
        Student student6 = new Student("啊啊啊");
        Student student7 = new Student("你行啊");
        Student student8 = new Student("A");
        Student student9 = new Student("A");
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        studentList.add(student5);
        studentList.add(student6);
        studentList.add(student7);
        studentList.add(student8);
        studentList.add(student9);

        List<Student> studentList1 = new ArrayList<>();
        studentList1.add(student1);
        studentList1.add(student2);
        studentList1.add(student3);
        studentList1.add(student4);
        studentList1.add(student8);
        List<Student> studentList2 = new ArrayList<>();
        studentList2.add(student5);
        studentList2.add(student6);
        studentList2.add(student7);
        List<Student> studentList3 = new ArrayList<>();
        studentList3.addAll(sort(studentList1));
        studentList3.addAll(sort(studentList2));
        for (Student s : studentList3) {
            ShowLogUtil.info(s);
        }
    }

    /**
     * 想要升序排序，前面值大于后面的值返回1，前面值小于后面的值返回-1
     * @param studentList
     * @return
     */
    private List<Student> sort(List<Student> studentList) {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                char[] c1 = PinyinUtil.getPingYin(o1.getName()).toCharArray();
                char[] c2 = PinyinUtil.getPingYin(o2.getName()).toCharArray();
                int maxLength = 0;
                int minLength = 0;
                maxLength = (c1.length > c2.length) ? c1.length : c2.length;
                minLength = (c1.length < c2.length) ? c1.length : c2.length;
                ShowLogUtil.info("====maxLength=" + maxLength);
                for (int i = 0; i < maxLength; i++) {
                    if (i == minLength) {
                        if (c1.length == minLength) {
                            ShowLogUtil.info("====i=" + i + "====(c1.length == minLength)====");
                            return -1;
                        }
                        if (c2.length == minLength) {
                            ShowLogUtil.info("====i=" + i + "====(c2.length == minLength)====");
                            return 1;
                        }
                    }
                    ShowLogUtil.info("====i=" + i + "====c1[i]=" + c1[i] + "====c2[i]=" + c2[i] + "====(c1[i] > c2[i])=" + (c1[i] > c2[i]));
                    if (c1[i] > c2[i]) {
                        ShowLogUtil.info("====" + o1.getName() + "====" + o2.getName() + "====1");
                        return 1;
                    } else if (c1[i] < c2[i]) {
                        ShowLogUtil.info("====" + o1.getName() + "====" + o2.getName() + "====-1");
                        return -1;
                    }

                }
                return 0;
            }
        });
        return studentList;
    }
}

